<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */


class SalaryInfoZJ extends CI_Controller{
    
    
    function __construct() {
        parent::__construct();
        
        $this->load->library('SalaryInfoZJ');
    }
    public  function index($userid){
        header("Access-Control-Allow-Origin:*");
        $this->load->library('SalaryInfoZJ');
        /*
        $one_date = date("Ym",strtotime("-1 month"));
        $two_date = date("Ym",strtotime("-2 month"));
        $three_date = date("Ym",strtotime("-3 month"));
        $four_date = date("Ym",strtotime("-4 month"));
        $five_date = date("Ym",strtotime("-5 month"));
         * *
         */
        //得到系统的年月  
        $tmp_date=date("Ym");  
        //切割出年份  
        $tmp_year=substr($tmp_date,0,4);  
        //切割出月份  
        $tmp_mon =substr($tmp_date,4,2);  
        
        $one_date    = date("Ym",mktime(0,0,0,$tmp_mon-1,1,$tmp_year));
        $two_date    = date("Ym",mktime(0,0,0,$tmp_mon-2,1,$tmp_year));
        $three_date  = date("Ym",mktime(0,0,0,$tmp_mon-3,1,$tmp_year));
        $four_date   = date("Ym",mktime(0,0,0,$tmp_mon-4,1,$tmp_year));
        $five_date   = date("Ym",mktime(0,0,0,$tmp_mon-5,1,$tmp_year));
        $res_dataone = $this->Data_model->get_single_data("GZ_YM = '".$one_date."' and A0190 = '".$userid."'","C21POOLZJ");
        $res_datatwo = $this->Data_model->get_single_data("GZ_YM = '".$two_date."' and A0190 = '".$userid."'","C21POOLZJ");
        $res_datathree = $this->Data_model->get_single_data("GZ_YM = '".$three_date."' and A0190 = '".$userid."'","C21POOLZJ");
        $res_datafour = $this->Data_model->get_single_data("GZ_YM = '".$four_date."' and A0190 = '".$userid."'","C21POOLZJ");
        $res_datafive = $this->Data_model->get_single_data("GZ_YM = '".$five_date."' and A0190 = '".$userid."'","C21POOLZJ");
        
        $res_dataone['href'] = "collapseOne";
        $res_datatwo["href"] = "collapseTwo";
        $res_datathree["href"] = "collapseThree";
        $res_datafour["href"] = "collapseFour";
        $res_datafive["href"] = "collapseFive";
        $res_dataone['date_show'] = $one_date;
        $res_datatwo["date_show"] = $two_date;
        $res_datathree["date_show"] = $three_date;
        $res_datafour["date_show"] = $four_date;
        $res_datafive["date_show"] = $five_date;
        $result_data = array(
            "data" => array(
                "1" => $res_dataone,
                "2" => $res_datatwo,
                "3" => $res_datathree,
                "4" => $res_datafour,
                "5" => $res_datafive
            )
        );
        $this->load->view("SalaryInfoZJ",$result_data);
        //$this->load->view("SalaryInfoZJ",$res);
    }
    
    /*
     * add by zhangxuelin 2017-9-4 OA移动门户跳转登录页
     */
    public function indexByUserid(){
        $userid = $_REQUEST['userid'];
        header("Access-Control-Allow-Origin:*");
        $this->load->library('SalaryInfoZJ');
        /*
        $one_date = date("Ym",strtotime("-1 month"));
        $two_date = date("Ym",strtotime("-2 month"));
        $three_date = date("Ym",strtotime("-3 month"));
        $four_date = date("Ym",strtotime("-4 month"));
        $five_date = date("Ym",strtotime("-5 month"));
         * *
         */
        //得到系统的年月  
        $tmp_date=date("Ym");  
        //切割出年份  
        $tmp_year=substr($tmp_date,0,4);  
        //切割出月份  
        $tmp_mon =substr($tmp_date,4,2);  
        
        $one_date    = date("Ym",mktime(0,0,0,$tmp_mon-1,1,$tmp_year));
        $two_date    = date("Ym",mktime(0,0,0,$tmp_mon-2,1,$tmp_year));
        $three_date  = date("Ym",mktime(0,0,0,$tmp_mon-3,1,$tmp_year));
        $four_date   = date("Ym",mktime(0,0,0,$tmp_mon-4,1,$tmp_year));
        $five_date   = date("Ym",mktime(0,0,0,$tmp_mon-5,1,$tmp_year));
        $res_dataone = $this->Data_model->get_single_data("GZ_YM = '".$one_date."' and A0190 = '".$userid."'","C21POOLZJ");
        $res_datatwo = $this->Data_model->get_single_data("GZ_YM = '".$two_date."' and A0190 = '".$userid."'","C21POOLZJ");
        $res_datathree = $this->Data_model->get_single_data("GZ_YM = '".$three_date."' and A0190 = '".$userid."'","C21POOLZJ");
        $res_datafour = $this->Data_model->get_single_data("GZ_YM = '".$four_date."' and A0190 = '".$userid."'","C21POOLZJ");
        $res_datafive = $this->Data_model->get_single_data("GZ_YM = '".$five_date."' and A0190 = '".$userid."'","C21POOLZJ");
        
        $res_dataone['href'] = "collapseOne";
        $res_datatwo["href"] = "collapseTwo";
        $res_datathree["href"] = "collapseThree";
        $res_datafour["href"] = "collapseFour";
        $res_datafive["href"] = "collapseFive";
        $res_dataone['date_show'] = $one_date;
        $res_datatwo["date_show"] = $two_date;
        $res_datathree["date_show"] = $three_date;
        $res_datafour["date_show"] = $four_date;
        $res_datafive["date_show"] = $five_date;
        $result_data = array(
            "data" => array(
                "1" => $res_dataone,
                "2" => $res_datatwo,
                "3" => $res_datathree,
                "4" => $res_datafour,
                "5" => $res_datafive
            )
        );
        $this->load->view("SalaryInfoZJ",$result_data);
    }

    public function Login(){
        if (isset($_GET['code'])){
            $code = $_GET['code'];
            $res = $this->Data_model->get_single_data(array('name'=>'中骏森驰',
                                                            'type'=>'SalaryInfoZJ_3'),"WEIXININFOZJ");
            $this->get_userId($res['access_token'], $code,$res['agentid']);
            
        }else{
            echo "NO CODE";
        }
    }
    
    function get_userId($access_token,$code,$agentId){
        if($access_token){
            $url = "https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo?access_token=".$access_token."&code=".$code."&agentid=".$agentId;
            $json=self::http_request_json($url);//这个地方不能用file_get_contents
            $data=json_decode($json,true);
            if(isset($data['errcode']) & $data['errcode'] != 0){
                if($data['errcode'] == "40001" or $data['errcode'] == '42001' or $data['errcode'] == '40014' or $data['errcode'] == '40029'){
                    $this->get_access_token();
                    
                    /*
                    $url = "https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo?access_token=".$access_token."&code=".$code."&agentid=".$agentId;
					$json=self::http_request_json($url);//这个地方不能用file_get_contents
					$data=json_decode($json,true);
					$this->index($data['UserId']);
                     
                     * 
                     */
                    
                    $newurl = "http://weixin.gensenauto.com/WEIXIN/SalaryInfoZJ/Login?code=".$code;  
                    echo "<script language='javascript' type='text/javascript'>";  
                    echo "window.location.href='".$newurl."'";  
                    echo "</script>"; 
                    
                }else{
                    echo "页面请求错误，请检查！";
                }
            }else{
                $this->index($data['UserId']);
            }
        }else{
            $this->get_access_token();
            $this->Login();
        }
    }
    function get_access_token(){
        $result = $this->Data_model->get_single_data(array('name'=>'中骏森驰',
                                                            'type'=>'SalaryInfoZJ_3'),"WEIXININFOZJ");
        $corpid = $result['corpid'];
        $corpsecret = $result['corpsecret'];
        $url="https://qyapi.weixin.qq.com/cgi-bin/gettoken?&corpid=".$corpid."&corpsecret=".$corpsecret;
        $json=self::http_request_json($url);//这个地方不能用file_get_contents
        $data=json_decode($json,true);
        if(isset($data['access_token'])){
            $this->Data_model->update_data(array('name'=>'中骏森驰','type'=>'SalaryInfoZJ_3'),array('access_token' => $data['access_token']),'WEIXININFOZJ');
        }else{
            return "获取access_token错误";
        }        
    }
    //因为url是https 所有请求不能用file_get_contents,用curl请求json 数据
    function http_request_json($url){  
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL,$url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $result = curl_exec($ch);
        curl_close($ch);
        return $result;  
    }
}
